import { reqDetail,reqShopAdd } from "../../request/api";
//初始值
const initState={
    detail:{}
}

//actionTypes
const types={
    CHANGE_DETAIL:"DETAIL_CHANGE_DETAIL"
}

//reducer 修改数据
const reducer=(state=initState,action)=>{
    switch(action.type){
        case types.CHANGE_DETAIL:
            return {
                ...state,
                detail:action.obj
            }
        default:
            return state;
    }
}

//actionCreators组件派发的动作
export const actions={
    changeDetail:(obj)=>({type:types.CHANGE_DETAIL,obj}),

    reqDetail:(id)=>(dispatch,getState)=>{
        reqDetail({id:id}).then(res=>{
            if(res.data.code===200){
                dispatch(actions.changeDetail(res.data.list[0]))
            }
        })
    },
    reqShopAdd:(id)=>(dispatch,getState)=>{
        reqShopAdd({uid:JSON.parse(sessionStorage.getItem("user")).uid,type:1,goodsid:id,num:1}).then(res=>{
            if(res.data.code===200){
                console.log("购物车添加成功");
            }
        })
    },
}

//selectors导出数据
export const getDetail=(state)=>state.detail.detail

export default reducer;
